//
//  ViewController.m
//  MutipeerConnectivity互传图片
//
//  Created by 美食家 on 16/7/15.
//  Copyright © 2016年 不孤独的美食家. All rights reserved.
//

#import "ViewController.h"
#import <MultipeerConnectivity/MultipeerConnectivity.h>

@interface ViewController ()<MCSessionDelegate,MCBrowserViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *showImg;
//通过MCAdvertiserAssistant能否使设备被搜索到，但要调用start方法，相反调用stop方法
@property(nonatomic,strong) MCAdvertiserAssistant* assistant;
//蓝牙会话，通过它发送数据。具体接收数据由代理去完成
@property(nonatomic,strong) MCSession* m_session;
//记录蓝牙设备ID
@property(nonatomic,strong) MCPeerID*  peerId;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MCPeerID* peerID= [[MCPeerID alloc]initWithDisplayName:[UIDevice currentDevice].name];
    self.m_session=[[MCSession alloc]initWithPeer:peerID];
    self.m_session.delegate=self;
}
#pragma mark - 点击事件响应方法
//查找蓝牙设备
- (IBAction)lookforBlueteeth:(id)sender {
    MCBrowserViewController *browser = [[MCBrowserViewController alloc]initWithServiceType:@"M_S_J" session:self.m_session];
    browser.delegate = self;
    [self presentViewController:browser animated:YES completion:nil];
}
//选择图片
- (IBAction)selectImg:(id)sender {
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:imgPicker animated:YES completion:nil];
}
//发送图片
- (IBAction)sendImg:(id)sender {
    if (!self.showImg.image)   return;
    // 发送数据
    [self.m_session sendData:UIImagePNGRepresentation(self.showImg.image) toPeers:@[self.peerId] withMode:MCSessionSendDataUnreliable error:nil];
}
//能否被周边蓝牙设备检测到
- (IBAction)isEnableSeek:(id)sender {
    UISwitch* swi=(UISwitch*)sender;
    if (swi.isOn) {
        self.assistant=[[MCAdvertiserAssistant alloc]initWithServiceType:@"M_S_J"
                                                    discoveryInfo:nil
                                                    session:self.m_session];
        [self.assistant start];
    }
}
#pragma mark - MCSessionDelegate代理方法
// 接收到的数据
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
    UIImage* image= [UIImage imageWithData:data];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.showImg.image = image;
    });
}

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
{
    
}

- (void)                    session:(MCSession *)session
  didStartReceivingResourceWithName:(NSString *)resourceName
                           fromPeer:(MCPeerID *)peerID
                       withProgress:(NSProgress *)progress
{

}

- (void)    session:(MCSession *)session
   didReceiveStream:(NSInputStream *)stream
           withName:(NSString *)streamName
           fromPeer:(MCPeerID *)peerID
{

}
- (void)                    session:(MCSession *)session
 didFinishReceivingResourceWithName:(NSString *)resourceName
                           fromPeer:(MCPeerID *)peerID
                              atURL:(NSURL *)localURL
                          withError:(nullable NSError *)error
{

}

#pragma mark - MCBrowserViewControllerDelegate代理方法
// 当另一设备同意连接后，调用的代理方法
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
    [browserViewController dismissViewControllerAnimated:YES completion:nil];
}

// 退出连接
- (void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController
{
    
}
// 连接带有peerID标识设备时调用的代理
- (BOOL)browserViewController:(MCBrowserViewController *)browserViewController
      shouldPresentNearbyPeer:(MCPeerID *)peerID
            withDiscoveryInfo:(nullable NSDictionary<NSString *, NSString *> *)info
{
    self.peerId = peerID;
    return YES;
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    self.showImg.image = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
